#!/bin/bash
script=$(realpath $0)
sp=$(dirname $script)

TTY=$1
rm /tmp/$1
baud=16384
for i in 4800 9600 19200 38400 57600 115200
do
  echo $i
  stty $i -F /dev/${TTY}
  timeout 10s ${sp}/check_baud.sh ${TTY}
  if [ $? -eq "1" ]; then 
	echo Test
	  baud=$i
  fi
done
#if [ -f /tmp/$1 ]; then
#	baud=$(cat /tmp/$1)
#fi

#exit 1
echo $baud
#exit 1

if [ $baud -ne 16384 ]; then
	echo ${TTY}
	echo $baud
	stty $baud -F /dev/${TTY}
    uid=$(echo $(udevadm info --name=/dev/${TTY}|sed -n '/MAJOR/{s/[A-Z: =]*//;p}')*256+$(udevadm info --name=/dev/${TTY}|sed -n '/MINOR/{s/[A-Z: =]*//;p}')|bc)
    mosquitto_pub -h 172.24.42.2 -t "gpsi/$(hostname)/${uid}/baud" -m ${baud}	
    idp=$(udevadm info -a -q property --export -n /dev/${TTY}|grep ID_MODEL_ID|cut -d= -f2|sed -e "s/'//g")
	idv=$(udevadm info -a -q property --export -n /dev/${TTY}|grep ID_VENDOR_ID|cut -d= -f2|sed -e "s/'//g")
   idpath=$(udevadm info -a -q property --export -n ${TTY}|grep ID_PATH=|cut -d= -f2|sed -e "s/'//g")
   echo $(date +%s),${TTY},${idp},${idv},$i
	nohup ${sp}/gps_raw5.sh ${TTY} &
fi

